import React, {Component} from 'react';
import './index.less';
import {
    base,
    high
} from 'nc-lightapp-front';
import Header from "../components/Header";
import SearchModal from "../components/SearchModal";
import RosterModal from "../components/RosterModal";
import DeleteConfirmModal from '../components/DeleteConfirmModal';
import CheckModal from '../components/CheckModal';
import getMainTableData from './functions/getMainTableData';
import MainTable from "../components/MainTable";
import toggleModal from "./functions/toggleModalVisible";
import processTemplate from "./functions/processTemplateData";
import updateButtonStatus from "./functions/updateButtonStatus";
import setButtonStatus from './functions/setButtonStatus';
import AddNew from "../components/AddNew";
import initCardForm from "./functions/initCardForm";
import getOnePsnInfo from "./functions/getOnePsnInfo";
import handleAllpks from "./functions/handleAllpks";
import clearMainTableData from "./functions/clearMainTableData";
import {getAppPageConfig} from "src/hrpub/common/utils/utils";

class Blacklist extends Component {
    constructor(props) {
        super(props);
        if (props.use) {
            props.use.form('blackcard');
            props.use.editTable('blacklist');
            props.use.search('blackquery');
        }
        this.config = {
            pagecode: getAppPageConfig().pagecode,
            appcode: getAppPageConfig().appcode,
            nodetype: props.nodetype
        };
        this.state = {
            nodetype: props.nodetype,
            orgVal: {},
            json: {},
            searchModalVisible: false,
            searchModalValue: {},
            rosterModalVisible: false,
            deleteConfirmModalVisible: false,
            checkModalVisible: false,
            queryOid: '',
            pageInfo: {
                pageSize: 10,
                pageIndex: 1,
                total: 0,
                totalPage: 1
            },
            hasSearched: false,
            includeCheckedPsn: false, //是否包含解除人员
            pk_psndoc_bad: null,
            page: 'main'
        };
        props.createUIDom(this.config, (data) => {
            this.setState({
                buttons: data.button || [],
                context: data.context || {},
                queryOid: data.template.blackquery.oid
            });

            data.template = processTemplate.call(this, data.template || {}, data.button || []);

            props.meta.setMeta(data && data.template ? data.template : {});
            props.button.setButtons(data && data.button ? data.button : {});
            updateButtonStatus.call(this);
            this.setButtonStatus('main');
            handleAllpks.set([]);
        });

        props.MultiInit.getMultiLang({
            moduleId: 'hi6007', domainName: 'hrhi', callback: (json, status, inlt) => {
                if (status) {
                    this.setState({json, inlt})//存json和inlt到页面state中并刷新页面
                }
            }
        });

        this.updateState = this.updateState.bind(this);
        this.getMainTableData = this.getMainTableData.bind(this);
        this.toggleModal = this.toggleModal.bind(this);
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.initCardForm = this.initCardForm.bind(this);
        this.getOnePsnInfo = this.getOnePsnInfo.bind(this);
    }

    componentDidMount() {
        /*let code = this.state.nodetype === 'GROUP_NODE' ? '60074010' : '60074020';
        if (window.location.href.match('localhost:3006')) window.location.hash = '?&c=' + code + '&p=' + code + 'p&ar=0001Z700APPN' + code;*/
    }

    updateOrgValue(value) {
        this.setState({
            orgVal: value
        }, () => {
            if (this.state.hasSearched) {
                if (!value.refpk) {
                    clearMainTableData.call(this);
                } else {
                    this.getMainTableData()
                }
            } else {
                updateButtonStatus.call(this);
            }
        })
    }

    setButtonStatus(page) {
        setButtonStatus.call(this, page);
    }

    updateState(data, callback = () => {
    }) {
        this.setState(data, callback)
    }

    getMainTableData() {
        getMainTableData.call(this);
    }

    // 切换各种弹窗显示隐藏，type为弹窗的显示隐藏state的key值
    toggleModal(type, visible) {
        return toggleModal.call(this, type, visible);
    }

    initCardForm() {
        initCardForm.call(this);
    }

    getOnePsnInfo() {
        getOnePsnInfo.call(this);
    }

    render() {
        const {
            orgVal, queryOid, pageInfo, rosterModalVisible, page,
            includeCheckedPsn, hasSearched, pk_psndoc_bad, deleteConfirmModalVisible,
            checkModalVisible, json
        } = this.state;
        return (
            <div className='entry-staff-query nc-bill-card'>
                <Header {...this.props}
                        json={json}
                        page={page}
                        getMainTableData={this.getMainTableData}
                        orgVal={orgVal}
                        hasSearched={hasSearched}
                        pageInfo={pageInfo}
                        includeCheckedPsn={includeCheckedPsn}
                        updateState={this.updateState}
                        updateOrgValue={this.updateOrgValue}
                        initCardForm={this.initCardForm}
                        pk_psndoc_bad={pk_psndoc_bad}
                        ref={node => this.headerNode = node}
                />

                <div style={{display: page === 'main' ? '' : 'none'}}
                     className="entry-staff-query-content">
                    <MainTable
                        {...this.props}
                        json={json}
                        updateState={this.updateState}
                        getMainTableData={this.getMainTableData}
                        pageInfo={pageInfo}
                        initCardForm={this.initCardForm}
                        includeCheckedPsn={includeCheckedPsn}
                        pk_psndoc_bad={pk_psndoc_bad}
                        orgVal={orgVal}
                    />
                </div>

                {page === 'main' ? null :
                    <AddNew {...this.props} includeCheckedPsn={includeCheckedPsn} json={json}
                            page={page} orgVal={orgVal} pk_psndoc_bad={pk_psndoc_bad}/>}

                <SearchModal
                    {...this.props}
                    json={json}
                    pageInfo={pageInfo}
                    queryOid={queryOid}
                    updateState={this.updateState}
                    getMainTableData={this.getMainTableData}
                />

                <RosterModal
                    {...this.props}
                    orgVal={orgVal}
                    json={json}
                    includeCheckedPsn={includeCheckedPsn}
                    visible={rosterModalVisible}
                    onClose={this.toggleModal('rosterModalVisible', false)}
                />
                <DeleteConfirmModal
                    {...this.props}
                    json={json}
                    updateState={this.updateState}
                    deleteConfirmModalVisible={deleteConfirmModalVisible}
                    pk_psndoc_bad={pk_psndoc_bad}
                    getMainTableData={this.getMainTableData}
                    orgVal={orgVal}
                    page={page}
                    getOnePsnInfo={this.getOnePsnInfo}
                />

                <CheckModal
                    {...this.props}
                    json={json}
                    updateState={this.updateState}
                    checkModalVisible={checkModalVisible}
                    includeCheckedPsn={includeCheckedPsn}
                    pk_psndoc_bad={pk_psndoc_bad}
                    orgVal={orgVal}
                    page={page}
                    getMainTableData={this.getMainTableData}
                    getOnePsnInfo={this.getOnePsnInfo}
                />
            </div>
        );
    }
}

export default Blacklist;
